using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Facebook.Entity;
using Microsoft.WindowsMobile.PocketOutlook;

namespace FacebookDeviceSample
{
    public partial class Form1 : Form
    {
        private OutlookSession session = new OutlookSession();
        private string facebookPath;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            facebookService1.ApplicationKey = "8141812bae6d6912fa098a804f53389e";
            facebookService1.Secret = "3bcc261c1ffc6c549b7da1e1b79b9f46";

            try
            {
                Collection<User> friends = facebookService1.GetFriends();
                Collection<FacebookEvent> events = facebookService1.GetEvents();
                Collection<Album> albums = facebookService1.GetPhotoAlbums();
                Notifications n = facebookService1.GetNotifications();
                User me = facebookService1.GetUserInfo();
                //facebookService1.CreateAlbum("test", "test", "test");
                lbFriends.DataSource = friends;
                lbFriends.DisplayMember = "Name";
                lbFriends.ValueMember = "UserId";
                lbEvents.DataSource = events;
                lbEvents.DisplayMember = "Name";
                lbEvents.ValueMember = "EventId";
                lbAlbums.DataSource = albums;
                lbAlbums.DisplayMember = "Name";
                lbAlbums.ValueMember = "AlbumId";

                TreeNode rootNode = tvNotifications.Nodes.Add("Notifications");
                TreeNode pokeNode = rootNode.Nodes.Add("Pokes (" + n.UnreadPokeCount.ToString() + ")");
                if (n.UnreadPokeCount > 0)
                {
                   pokeNode.Nodes.Add(n.MostRecentPokeId);
                }
                TreeNode msgNode = rootNode.Nodes.Add("Messages (" + n.UnreadMessageCount.ToString() + ")");
                if (n.UnreadMessageCount > 0)
                {
                    msgNode.Nodes.Add(n.MostRecentMessageId);
                }
                TreeNode shareNode = rootNode.Nodes.Add("Shares (" + n.UnreadShareCount.ToString() + ")");
                if (n.UnreadShareCount > 0)
                {
                    shareNode.Nodes.Add(n.MostRecentShareId);
                }
                TreeNode groupNode = rootNode.Nodes.Add("Group Invitations (" + n.GroupInvites.Count.ToString() + ")");
                foreach (string g in n.GroupInvites)
                {
                    groupNode.Nodes.Add(g);
                }
                TreeNode eventsNode = rootNode.Nodes.Add("Event Invitations (" + n.EventInvites.Count.ToString() + ")");
                foreach (string ei in n.EventInvites)
                {
                    eventsNode.Nodes.Add(ei);
                }
                TreeNode friendNode = rootNode.Nodes.Add("Friend Requests (" + n.FriendRequests.Count.ToString() + ")");
                foreach(string f in n.FriendRequests)
                {
                    friendNode.Nodes.Add(f);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }

            facebookPath = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Facebook");
            if (!System.IO.Directory.Exists(facebookPath))
            {
                System.IO.Directory.CreateDirectory(facebookPath);
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            facebookService1.CreateAlbum(txtAlbum.Text, null, null);
            lbAlbums.DataSource = facebookService1.GetPhotoAlbums();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (lbAlbums.SelectedIndex > -1)
            {
                Album a = (Album)lbAlbums.SelectedItem;

                string albumPath = System.IO.Path.Combine(facebookPath, a.AlbumId);
                if (!System.IO.Directory.Exists(albumPath))
                {
                    System.IO.Directory.CreateDirectory(albumPath);
                }
                //Album a = (Album)lbAlbums.SelectedItem;
                Collection<Photo> photos = facebookService1.GetPhotos(a.AlbumId);
                foreach (Photo p in photos)
                {
                    p.PictureBig.Save(System.IO.Path.Combine(albumPath, System.IO.Path.GetFileName(p.PictureBigUrl.LocalPath)), System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }

        private void mnuAddToContacts_Click(object sender, EventArgs e)
        {
            if (lbFriends.SelectedIndex > -1)
            {
                User u = (User)lbFriends.SelectedItem;
                Contact c = session.Contacts.Items.AddNew();
                c.FirstName = u.FirstName;
                c.LastName = u.LastName;
                string pictureFile = System.IO.Path.Combine(facebookPath, u.UserId.ToString() + ".jpg");
                u.PictureSmall.Save(pictureFile, System.Drawing.Imaging.ImageFormat.Jpeg);

                c.SetPicture(pictureFile);
                if(u.WorkHistory.Count > 0)
                {
                    c.CompanyName = u.WorkHistory[0].CompanyName;
                }
                //c.Birthday = u.Birthday;
                c.Update();

            }
        }

        /*private void mnuAddToCalendar_Click(object sender, EventArgs e)
        {
            if (lbEvents.SelectedIndex > -1)
            {
                FacebookEvent fe = (FacebookEvent)lbEvents.SelectedItem;
                Collection<EventUser> attendees = facebookService1.GetEventMembers(fe.EventId);

                Appointment a = session.Appointments.Items.AddNew();
                a.Subject = fe.Name;
                a.Location = fe.Location;
                a.Body = fe.Description;
                a.Start = fe.StartDate;
                a.End = fe.EndDate;
                foreach (EventUser eu in attendees)
                {
                    if (eu.Attending == RSVPStatus.Attending)
                    {
                        Recipient r = a.Recipients.Add(new Recipient(eu.User.Name, eu.UserId));
                    }
                }
                a.Update();

                Collection<PublishImage> images = new Collection<PublishImage>();
                images.Add(new PublishImage("\\windows\\ppc.gif", "http://www.microsoft.com/windowsmobile"));

                facebookService1.PublishStory("Test story", "Generated by my Windows Mobile device", images);
            }
        }*/

        private void mnuProfile_Click(object sender, EventArgs e)
        {
            if (lbFriends.SelectedIndex > -1)
            {
                System.Diagnostics.Process.Start("http://m.facebook.com/profile.php?id=" + ((User)lbFriends.SelectedItem).UserId, "");
            }
        }
    }
}